<?php

class user extends CI_Controller
{
	 

	
	function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->helper(array('form','url'));

	}
	function index()
	{
		$data['users'] = $this->db->get('ciuser')->result_array();
		
		$this->load->view('manage/header');
		$this->load->view('user/index',$data);
		$this->load->view('manage/footer');
		
	}
	
	function edituser()
	{
		$uid = $this->uri->segment(4);
		$this->load->library('form_validation');
		
		if(intval($uid)){
			$data['user'] = $this->db->where('uid',$uid)->get('ciuser')->result_array();
			$this->load->view('manage/header');
			$this->load->view('user/edit',$data);
			$this->load->view('manage/footer');
		}else{
			$this->load->model('model_user','',true);
			$this->model_user->update();
			redirect('user/index');
		}
	}
	
	function adduser()
	{
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('username','username','required');
		$this->form_validation->set_rules('password','password','required');
		
		if($this->form_validation->run() == false){
			$this->load->view('manage/header');
			$this->load->view('user/post');
			$this->load->view('manage/footer');
		}else{
			$this->load->model('model_user','',TRUE);
			$this->model_user->save();
			redirect('user/index');
		}
	}
	
	function dropuser()
	{
		$uid = $this->uri->segment(4);
		if($uid){
			$this->load->model('model_user','',true);
			$this->model_user->delete(intval($uid));
			redirect('user/index');
		}else{
			echo $this->uri->segment(4)."what"; //test
		}
	}
	
	function register()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username','username','required');
		$this->form_validation->set_rules('password', 'password','required');
		
		if($this->form_validation->run() == false){
			
			$this->load->view('manage/index');
			$this->load->view('user/register');
			$this->load->view('manage/footer');
		}else{
			$this->load->model('model_user',"",True);
			$this->model_user->save();
			$this->load->view('success');
		}
	}
	
	function login()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username','username','required');
		$this->form_validation->set_rules('password', 'password','required');
		
		if($this->form_validation->run() == false){
			
			//$this->load->view('manage/index');
			$this->load->view('user/login');
			//$this->load->view('manage/footer');
			
		}else{
			$this->load->model('model_user', '', true);
			if($this->model_user->checklogin()){
				
				$this->session->set_userdata('role',"manage");
				redirect('/article/index','refresh');
			}else{
				echo "404 error";
				redirect('/home/index','refresh');
			}
		}
	}
	
	function logout()
	{
		$this->session->set_userdata('role','anonymous');
		redirect('/home/index','refresh');
	}
	
	function changepassword()
	{
		
	}
	
	function deluser()
	{
		
	}
}